package com.kamloo.stg.fragment;

import java.util.ArrayList;

import com.kamloo.stg.adapter.TabBookmarksScheduleArrayAdapter;
import com.kamloo.stg.data.db.*;
import com.kamloo.stg.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TabBookmarksSchedule extends Fragment {
	private int m_nodeId;
	private int m_periodId;	
	private boolean m_isBookmark;
	private TextView m_scheduleLine;
	private TextView m_scheduleStop;
	private TextView m_schedulePeriod;
	private ListView m_scheduleList;
	private TextView m_scheduleNoData;	
	private ImageButton m_scheduleBookmark;
	ArrayList<String> m_values;
	
	OnClickListener bookmarkClickListener = new OnClickListener() {		
		public void onClick(View view) {
			BookmarkDAO bookmarkDao = new BookmarkDAO(getActivity());
			bookmarkDao.open();
			
			if (m_isBookmark) {
				Bookmark bookmark = bookmarkDao.select(m_nodeId);
				bookmarkDao.delete(bookmark.getId());
				m_scheduleBookmark.setImageResource(R.drawable.alarm_bw);
				m_isBookmark = false;
			} else {
				bookmarkDao.add(m_nodeId);
				m_scheduleBookmark.setImageResource(R.drawable.alarm);
				m_isBookmark = true;
			}
			
			bookmarkDao.close();
		}		
	};
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (container == null) {
			return null;
		} 
		return (RelativeLayout) inflater.inflate(R.layout.tab_bookmarks_schedule, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		m_scheduleList = (ListView) view.findViewById(R.id.tabBookmarksScheduleList);
		m_scheduleNoData = (TextView) view.findViewById(R.id.tabBookmarksScheduleNoData);
		m_scheduleLine = (TextView) view.findViewById(R.id.tabBookmarksScheduleLineInfo);
		m_scheduleStop = (TextView) view.findViewById(R.id.tabBookmarksScheduleStopInfo);		
		m_schedulePeriod = (TextView) view.findViewById(R.id.tabBookmarksSchedulePeriodInfo);
		m_scheduleBookmark = (ImageButton) view.findViewById(R.id.tabBookmarksScheduleFavorite);
	}
	
	public void initializeView(int nodeId, int periodId) {
		m_nodeId = nodeId;
		m_periodId = periodId;
		m_values = new ArrayList<String>();
		
		setupBookmark();
		setScheduleData();
		
		if (m_values.size() != 0) {
			TabBookmarksScheduleArrayAdapter adapter = new TabBookmarksScheduleArrayAdapter(getActivity(), m_values.toArray(new String[m_values.size()]));
			m_scheduleList.setAdapter(adapter);

			m_scheduleLine.setVisibility(View.VISIBLE);
			m_scheduleLine.setText(getResources().getString(R.string.tabBookmarks_Schedule_LineInfo) + " " + getLineName());
			m_scheduleStop.setVisibility(View.VISIBLE);
			m_scheduleStop.setText(getResources().getString(R.string.tabBookmarks_Schedule_StopInfo) + " " + getStopName());
			m_schedulePeriod.setVisibility(View.VISIBLE);
			m_schedulePeriod.setText(getResources().getString(R.string.tabBookmarks_Schedule_PeriodInfo) + " " + getPeriodName());
			m_scheduleBookmark.setVisibility(View.VISIBLE);
			m_scheduleBookmark.setOnClickListener(bookmarkClickListener);

		} else {
			m_scheduleNoData.setVisibility(View.VISIBLE);
		}		
	}
	
	private void setupBookmark() {
		BookmarkDAO bookmarkDao = new BookmarkDAO(getActivity());
		bookmarkDao.open();		
		m_isBookmark = bookmarkDao.isFavorite(m_nodeId);	
		if (m_isBookmark) {
			m_scheduleBookmark.setImageResource(R.drawable.alarm);
		}
		else {
			m_scheduleBookmark.setImageResource(R.drawable.alarm_bw);
		}
		bookmarkDao.close();
	}
	
	private String getStopName() {
		NodeDAO nodeDao = new NodeDAO(getActivity());
		StopDAO stopDao = new StopDAO(getActivity());
		nodeDao.open();
		stopDao.open();
		
		Node node = nodeDao.select(m_nodeId);
		Stop stop = stopDao.select(node.getFk_stopId());
		
		stopDao.close();
		nodeDao.close();
		
		return stop.getName();
	}
	
	private String getLineName() {
		NodeDAO nodeDao = new NodeDAO(getActivity());
		LineDAO lineDao = new LineDAO(getActivity());
		DirectionDAO directionDao = new DirectionDAO(getActivity());
		nodeDao.open();
		lineDao.open();
		directionDao.open();
		
		Node node = nodeDao.select(m_nodeId);
		Direction direction = directionDao.select(node.getFk_directionId());
		Line line = lineDao.select(direction.getFk_lineId());
		
		directionDao.close();
		lineDao.close();
		nodeDao.close();
		
		String name = line.getIdentifier();
		if (name.contains("STS"))
	    {
			name = name.replace("STS", "");
			name = name.substring(0, name.indexOf("_"));
	    }
		
		return name;
	}
	
	private String getPeriodName() {
		//PeriodDAO periodDao = new PeriodDAO(getActivity());
		//periodDao.open();		
		//Period period = periodDao.select(m_periodId);		
		//periodDao.close();		
		//return period.getName();
		
		switch (m_periodId) {
		case 0:
			return getString(R.string.period_Monday_Friday);
		case 1:
			return getString(R.string.period_Summer);
		case 2:
			return getString(R.string.period_Saturday);
		case 3: 
			return getString(R.string.period_Sunday);
		}
		
		return "";
	}
	
	private void setScheduleData() {
		ScheduleDAO scheduleDao = new ScheduleDAO(getActivity());
    	scheduleDao.open();
    	
    	ArrayList<Schedule> schedules = scheduleDao.select(m_nodeId, m_periodId);;
    	for (Schedule schedule : schedules) {
    		
    		String value = "";
			if (schedule.getHour() < 10) {
				value += "0" + schedule.getHour();
			} else {
				value += schedule.getHour();
			}
			value += ":";
			if (schedule.getMinute() < 10) {
				value += "0" + schedule.getMinute();
			} else {
				value += schedule.getMinute();
			}
			
    		m_values.add(value);    		
    	}
    	
    	scheduleDao.close();
	}
	
}